package com.yl.wanandroid_mvvm_kotlin_jetpack

import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.alibaba.android.arouter.facade.annotation.Route
import com.yl.common.base.BaseActivity
import com.yl.common.exp.Constants
import com.yl.common.util.KVUtil
import com.yl.common.util.ToastUtil
import com.yl.home.ui.HomeFragment
import com.yl.mine.ui.MineFragment
import com.yl.navigation.ui.NavigationFragment
import com.yl.projects.ui.ProjectsFragment
import com.yl.wanandroid_mvvm_kotlin_jetpack.databinding.ActivityMainBinding

@Route(path = Constants.MAIN_ACTIVITY)
class MainActivity : BaseActivity<ActivityMainBinding>() {

    lateinit var fragmentList: MutableList<Fragment>
    var oldFragmentIndex: Int = 0

    private var homeFragment: HomeFragment? = null
    private var navigationFragment: NavigationFragment? = null
    private var projectsFragment: ProjectsFragment? = null
    private var mineFragment: MineFragment? = null

    override fun getLayoutID() = R.layout.activity_main

    override fun init() {
        homeFragment = HomeFragment()
        navigationFragment = NavigationFragment()
        projectsFragment = ProjectsFragment()
        mineFragment = MineFragment()

        mBind.imgDraw.setOnClickListener {
            mBind.dl.open()
        }

        mBind.floatingActionBtn.run {
            setOnClickListener(onFABClickListener)
        }
        //底部button显示文字和图标
        mBind.bnv.setOnItemSelectedListener {

            when (it.itemId) {
                R.id.f1 -> {
                    mBind.floatingActionBtn.visibility = View.VISIBLE
                    switchFragment(0)
                    mBind.fName = "首页"
                    return@setOnItemSelectedListener true
                }
                R.id.f2 -> {
                    mBind.floatingActionBtn.visibility = View.GONE
                    switchFragment(1)
                    mBind.fName = "导航"
                    return@setOnItemSelectedListener true
                }
                R.id.f3 -> {
                    mBind.floatingActionBtn.visibility = View.GONE
                    switchFragment(2)
                    mBind.fName = "项目"
                    return@setOnItemSelectedListener true
                }
                R.id.f4 -> {
                    mBind.floatingActionBtn.visibility = View.GONE
                    switchFragment(3)
                    mBind.fName = "我的"
                    return@setOnItemSelectedListener true
                }
            }
            true
        }
        //侧边栏
        initSidebar()
    }

    private fun initSidebar() {
        var rank = findViewById<ImageView>(R.id.rank)
        rank.setOnClickListener {
            ToastUtil.showMsg("排行榜")
        }
        var l4 = findViewById<LinearLayout>(R.id.l4)
        l4.setOnClickListener {
            val getIsNightMode = KVUtil.getBoolean(Constants.NIGHT_MODE)
            mBind.nightMode = getIsNightMode
            Log.e("Tag", "$getIsNightMode")
            if (!getIsNightMode) {
                KVUtil.put(Constants.NIGHT_MODE, true)
                setViewColor(R.color.white)
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
            } else {
                KVUtil.put(Constants.NIGHT_MODE, false)
                setViewColor(R.color.night_no)
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            }
            switchFragment(0)
            recreate()
        }
    }

    private fun setViewColor(color: Int) {
        var ns = findViewById<NestedScrollView>(R.id.ns)
        ns.setBackgroundColor(this.resources.getColor(color))
        mBind.bnv.setBackgroundColor(this.getResources().getColor(color))
    }


    override fun initData() {
        fragmentList = mutableListOf(
            homeFragment!!,
            navigationFragment!!,
            projectsFragment!!,
            mineFragment!!,
        )

        switchFragment(0)
    }

    override fun onDestroy() {
        super.onDestroy()
        homeFragment = null
        navigationFragment = null
        projectsFragment = null
        mineFragment = null

    }

    private var exitTime: Long = 0
    override fun onBackPressed() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            ToastUtil.showMsg("再按一次退出")
            exitTime = System.currentTimeMillis()
        } else {
            super.onBackPressed()
        }
    }

    private fun hintFragment(ft: FragmentTransaction, targetFragment: Fragment) {
        val fragments = supportFragmentManager.fragments
        for (fragment in fragments) {
            if (targetFragment != fragment) {
                ft.remove(fragment)
            }
        }
    }

    private fun switchFragment(position: Int) {
        //修改状态栏颜色
        window.statusBarColor = resources.getColor(com.yl.common.R.color.theme_color)
        val ft = supportFragmentManager.beginTransaction()
        val targetFragment = fragmentList[position]
        hintFragment(ft, targetFragment)
        val oldFragment = fragmentList[oldFragmentIndex]
        oldFragmentIndex = position

        if (oldFragment.isAdded) {
            ft.hide(oldFragment)
        }
        if (!targetFragment.isAdded) {
            ft.add(R.id.f_content, targetFragment)
        }

        ft.show(targetFragment).commitAllowingStateLoss()
    }


    /**
     * FAB 监听
     */
    private val onFABClickListener = View.OnClickListener {
        when (oldFragmentIndex) {
            0 -> {
                homeFragment!!.scrollToTop()
            }
            1 -> {
                navigationFragment!!.scrollToTop()
            }
            2 -> {
//                projectsFragment!!.scrollToTop()
            }
            3 -> {
//                mineFragment!!.scrollToTop()
            }
        }
    }

}